#include "useritem.h"
#include "ui_useritem.h"
#include<QBitmap>
UserItem::UserItem(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::UserItem)
{
    ui->setupUi(this);
}

UserItem::~UserItem()
{
    delete ui;
}
//设置用户信息
void UserItem::slot_setInfo(int id, QString name, int status, int icon, QString feeling)
{
    //0是用户的item，1是群聊的item
    if(m_type==0){
        //1、保存用户信息
        m_id =id;
        m_name = name;
        m_status = status;
        m_icon = icon;
        m_feeling = feeling;
        //2、设置昵称和签名
        ui->lb_name->setText(m_name);
        ui->lb_felling->setText(m_feeling);

        //3、判断用户是否在线
        QString iconPath = QString(":/tx/%1.png").arg(m_icon);
        if(m_status){
            //用户在线显示彩色头像
            ui->pb_icon->setIcon(QIcon(iconPath));
            ui->pb_icon->setIconSize(QSize(40,40));
        }else{
            //不在线 显示灰色头像
            QBitmap bitmap ; // 位图
            bitmap.load(iconPath);
            ui->pb_icon->setIcon(bitmap);
            ui->pb_icon->setIconSize(QSize(40,40));
        }
        //4、重绘
        this->repaint();
    }else if(m_type ==1){
        //1.保存群信息
        m_id = id;
        m_name = name;
        m_icon = icon;
        //2、设置昵称和签名
        ui->lb_name->setText(m_name);
        ui->lb_felling->setText(m_feeling);
        //设置在线彩色头像
        QString iconPath = QString(":/tx/%1.png").arg(m_icon);
        ui->pb_icon->setIcon(QIcon(iconPath));
        ui->pb_icon->setIconSize(QSize(40,40));
        //重绘
        this->repaint();
    }
}
//设置用户下线
void UserItem::slot_setUserOffline()
{
    //1、用户状态改成下线
    m_status = 0;
    //2、头像变灰
    QString iconPath = QString(":/tx/%1.png").arg(m_icon);
    QBitmap bitmap ; // 位图
    bitmap.load(iconPath);
    ui->pb_icon->setIcon(bitmap);
    ui->pb_icon->setIconSize(QSize(40,40));
    //4、重绘
    this->repaint();
}
//点击用户头像槽函数
void UserItem::on_pb_icon_clicked()
{
    //0是用户的item，1是群聊的item
    if(m_type==0){
        //点击头像发送信号
        Q_EMIT SIG_userItemClicked(m_id);
    }else if(m_type == 1)
        Q_EMIT SIG_groupItemClicked(m_id);

}

